<template>
  <el-dialog v-model="visible" title="核销明细" width="1200">
    <el-table
      border
      stripe
      :data="tableData"
      style="max-height: 450px"
      v-loading="loading"
    >
      <el-table-column type="index" label="序号" width="55" />
      <el-table-column prop="breedName" label="品种名称" />
      <el-table-column prop="labelCode" label="标签条编码" />
      <el-table-column prop="labelName" label="标签条名称" />
      <el-table-column prop="custPoints" label="客户积分" />
      <el-table-column prop="salemanPoints" label="业务员积分" />
      <el-table-column prop="cancelDate" label="核销时间" width="180">
        <template v-slot:default="{ row }">
          {{
            row.cancelDate
              ? dayjs(row.cancelDate).format("YYYY-MM-DD HH:mm:ss")
              : ""
          }}
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      background
      class="mt-4"
      :total="total"
      :page-size="queryData.pageSize"
      :page-sizes="[10, 20, 50, 100]"
      layout="total, prev, pager, next"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
  </el-dialog>
</template>
<script setup>
import { nextTick, ref } from "vue";
import { dayjs } from "element-plus";
import api from "@/api/report.js";

const props = defineProps({
  queryData: { type: Object, default: () => ({}) },
  dealerCode: { type: String, default: "" },
});

const visible = ref(false);
const queryData = ref({
  pageNo: 1,
  pageSize: 10,
});
const loading = ref(false);
const tableData = ref([]);
const total = ref(0);

const initData = async () => {
  loading.value = true;
  try {
    const res = await api.dealerDetail({
      ...props.queryData,
      ...queryData.value,
    });
    tableData.value = res.data.list;
    total.value = res.data.total;
  } finally {
    loading.value = false;
  }
};

const handleCurrentChange = (val) => {
  queryData.value.pageNo = val;
  initData(queryData.value);
};
const handleSizeChange = (val) => {
  queryData.value.pageSize = val;
  initData(queryData.value);
};

const openDialog = (row) => {
  queryData.value.dealerCode = "";
  tableData.value = [];
  total.value = 0;
  visible.value = true;
  queryData.value.dealerCode = row.dealerCode;
  nextTick(() => {
    initData(queryData.value);
  });
};

defineExpose({
  openDialog,
});
</script>
